#!/bin/bash

IMAGE_DIR=$1
IMAGE_FILE=$1/image-cpld.svf
MANIFEST_FILE=$1/MANIFEST
JTAG_DEVICE=/dev/jtag0

current_version=""
cpdate_version=""

verify_cpld_version()
{
    FILE=$IMAGE_DIR
    ID=${FILE##*/}
    OBJ='xyz.openbmc_project.Software.BMC.Updater'
    OBJ_PATH="/xyz/openbmc_project/software/$ID"
    INTERFACE='xyz.openbmc_project.Software.Version'
    PROPERTY='Version'
    CMD="busctl get-property  $OBJ $OBJ_PATH $INTERFACE $PROPERTY"
    str=$($CMD)
    str=${str#*\"}
    update_version=${str%%\"*}

    OBJ_PATH="/xyz/openbmc_project/software/cpld_active"
    CMD="busctl get-property  $OBJ $OBJ_PATH $INTERFACE $PROPERTY"
    str=$($CMD)
    str=${str#*\"}
    current_version=${str%%\"*}
    
    if [ $current_version = $update_version ]; then
        echo "Update version $update_version is currently running on the host"
        return 1
    fi

    return 0
}

# Check the power status of the host
check_host_state()
{
    host_service="xyz.openbmc_project.State.Host"
    host_path="/xyz/openbmc_project/state/host0"
    state_inft="xyz.openbmc_project.State.Host"
    state_property="CurrentHostState"

    host_state=`busctl get-property $host_service $host_path $state_inft $state_property`

    if [[ $host_state =~ "Running" ]]; then
        echo "Host is running, can not update cpld"
        return 1
    fi
}

generate_redfish_log()
{
    if [ ! -d "/var/log" ]; then
        mkdir -p /var/log
    fi

    OPENBMC_TAG="OpenBMC.0.1.$1"
    PARA=""
    shift
    while [ $# -ne 0 ]; do
        PARA=$PARA",$1"
        shift
    done
    logger -p local7.info "${OPENBMC_TAG}${PARA}"
}

check_cmd_state()
{
    if [ $? -eq 0 ]; then
        echo "$1 Successfully..."
        return 0
    else
        echo "$1 Failed..."
        generate_redfish_log "CPLD FirmwareUpdateFailed" "$current_version" "$update_version" "$1 Failed..."
        sleep 3 ##Response time reserved for front-end detection
                if [[ $2 != "NOT_EXIT" ]]; then
                    exit 1 ##exit script
                else
            return 1
        fi
    fi
}


#Set card or board cpld to update
setup_cpld_update()
{
	if [ "$1" == "card" ]
	then
		echo "jtag change to card"
		gpioutil set CARD_JTAG_SELECT 1
	fi
	
	if [ "$1" == "board" -a "$2" == "start" ]
	then
		gpioutil set CARD_JTAG_SELECT 0
		gpioutil set BOARD_JTAG_SELECT 1
	fi
    
    if [ "$1" == "board" -a "$2" == "stop" ]
	then
		gpioutil set CARD_JTAG_SELECT 1
		gpioutil set BOARD_JTAG_SELECT 0
	fi
    return 0
}

delete_update_file()
{
    FILE=$IMAGE_DIR
    ID=${FILE##*/}
    OBJ='xyz.openbmc_project.Software.BMC.Updater'
    OBJ_PATH="/xyz/openbmc_project/software/$ID"
    INTERFACE='xyz.openbmc_project.Object.Delete'
    METHOD='Delete'

    busctl call  $OBJ $OBJ_PATH $INTERFACE $ETHOD
    check_cmd_state "Delete FW:$ID form dbus interface"
    return 0
}

##Set cpld update dbus progress interface
## $1=Progress
set_dbus_progress_info()
{
    FILE=$IMAGE_DIR
    ID=${FILE##*/}
    OBJ='xyz.openbmc_project.Software.BMC.Updater'
    OBJ_PATH="/xyz/openbmc_project/software/$ID"
    INTERFACE='xyz.openbmc_project.Software.ActivationProgress'
    PROPERTY='Progress'

    busctl set-property  $OBJ $OBJ_PATH $INTERFACE $PROPERTY y $1
    check_cmd_state "Set FW:$ID progress=$1% progress to dbus interface"
#   echo "Set FW:$ID progress=$1%"
    return 0
}
##start update cpld
start_cpld_update()
{
    echo "use $JTAG_DEVICE to update cpld..."
    if [ ! -e $JTAG_DEVICE ]; then
        generate_redfish_log "FirmwareUpdateFailed" "$current_version" "$update_version" "cpld-update updated Failed :jtag device $JTAG_DEVICE does not exist"
        return 1
    fi

    #sleep 2
    cpld-update -d $JTAG_DEVICE -f 1000000 -s $IMAGE_FILE > /tmp/cpld_update_progress
    if [ $? -eq 0 ]; then
        echo "cpld updated successfully..."
    else
        echo "cpld updated Failed..."
        generate_redfish_log "FirmwareUpdateFailed" "$current_version" "$update_version" "flashcp updated Failed..."
        sleep 3 ##Response time reserved for front-end detection
        return 1
    fi

    return 0
}

#############################################################
#
#MIAN
#
#############################################################
echo "Cpld upgrade started at $(date)"

verify_cpld_version

if [ $? -ne 0 ]; then
    generate_redfish_log "FirmwareUpdateFailed" $firmware_type $update_version "Same version"
    exit 1
fi

check_host_state
if [ $? -ne 0 ]; then
    generate_redfish_log "FirmwareUpdateFailed" $firmware_type $update_version "Host is running"
    exit 1
fi

#Check the file to be burned
set_dbus_progress_info 21
if [ ! -e "$IMAGE_FILE" ]; then
    echo "Cpld update file $IMAGE_FILE doesn't exist"
    generate_redfish_log "FirmwareUpdateFailed" "$current_version" "$update_version" "Uploading the wrong file,cpld update file $IMAGE_FILE doesn't exist"
    sleep 3 ##Response time reserved for front-end detection
    exit 1
fi

setup_cpld_update $2 "start"

set_dbus_progress_info 25

#update cpld.
echo "CPLD update:update file is $IMAGE_FILE"

start_cpld_update

set_dbus_progress_info 100

setup_cpld_update $2 "stop"

generate_redfish_log "FirmwareUpdateCompleted" "$current_version" "$update_version"
echo "Cpld update finish"
/usr/local/cpld-version.sh
echo "Cpld update version"
